package com.agileai.crm.module.mytasks.handler;

import java.util.Date;

import com.agileai.common.KeyGenerator;
import com.agileai.crm.cxmodule.ColdCallsManage;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

/**
 * generated by miscdp
 */
public class PostponeManageHandler extends SimpleHandler{
	public PostponeManageHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		User user = (User)getUser();
		String dateTime = DateUtil.getDateByType(DateUtil.YYMMDDHHMISS_HORIZONTAL, new Date());
		param.put("PROCUST_VISIT_FILL_ID", user.getUserId());
		param.put("PROCUST_VISIT_FILL_NAME", user.getUserName());
		param.put("PROCUST_VISIT_FILL_TIME", dateTime);
		param.put("PROCUST_VISIT_DATE", dateTime.subSequence(0, 10));
		this.setAttributes(param);
		this.processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
	
	protected void processPageAttributes(DataParam param){
        setAttribute("PROCUST_VISIT_EFFECT",
                FormSelectFactory.create("VISIT_EFFECT")
                                 .addSelectedValue(getOperaAttributeValue("PROCUST_VISIT_EFFECT",
                                                                          "")));
   setAttribute("PROCUST_VISIT_TYPE",
           FormSelectFactory.create("VISIT_TYPE")
                            .addSelectedValue(getOperaAttributeValue("PROCUST_VISIT_TYPE",
                                                                     "")));
	}	
	
	public ViewRenderer doSaveAction(DataParam param){
		String rspText = FAIL;
		User user = (User) getUser();
		String custId = "";
		DataRow dataRow = getService().getCustInfoRecord(param);
		if(dataRow != null && !dataRow.isEmpty()){
			custId = dataRow.getString("CUST_ID");
		}else{
			custId = param.get("CUST_ID");
		}
		
		if(StringUtil.isNotNullNotEmpty(custId)){
			DataParam custParam = new DataParam();
			custParam.put("CUST_ID", custId);
			custParam.put("CUST_STATE", "Postpone");
			getService().updateCustStateRecord(custParam);
		}
		
		if(StringUtil.isNotNullNotEmpty(custId)){
			DataRow proCustRow = getService().getProCustRecord(new DataParam("CUST_ID",custId));
			if(proCustRow != null && !proCustRow.isEmpty()){
				
			}else{
				String orgId = KeyGenerator.instance().genKey();
				param.put("ORG_ID", orgId);
				String dateTime = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL, new Date());
				DataParam proCustParam = new DataParam();
				proCustParam.put("ORG_ID", orgId);
				proCustParam.put("ORG_NAME", param.get("CUST_NAME"));
				proCustParam.put("ORG_STATE", "0");
				proCustParam.put("ORG_SALESMAN", user.getUserId());
				proCustParam.put("ORG_CREATER", user.getUserId());
				proCustParam.put("ORG_CREATE_TIME", dateTime);
				proCustParam.put("ORG_UPDATE_TIME", dateTime);
				proCustParam.put("CUST_ID", custId);
				
				getService().createProCustInfoRecord(proCustParam);
				getService().updateTaskOrgIdRecord(param);
			}
		}
		
		getService().createProVisitRecord(param);
		
		DataRow taskRow = getService().getMasterRecord(param);
		String taskFollowState = taskRow.getString("TASK_FOLLOW_STATE");
    	if("Init".equals(taskFollowState)){
    		param.put("TASK_FOLLOW_STATE", "HaveFollowUpInit");
    	}else{
    		param.put("TASK_FOLLOW_STATE", "HaveFollowUp");
    	}
    	getService().updateTaskStateRecord(param);
    	
		param.put("TASK_CLASS", "ColdCalls");
		getService().updateTaskClassRecord(param);
		
		rspText = SUCCESS;
		return new AjaxRenderer(rspText);
	}
	
    protected ColdCallsManage getService() {
        return (ColdCallsManage) this.lookupService(ColdCallsManage.class);
    }
}

